660C - Hard Process - CodeForces Solution


binary search dp two pointers *1600

Please click on ads to support us..

C++ Code:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[300005];
int n,k;
int l,mid,r,ans;
int head,tail;//head和tail记录所需数列的首尾 
int mark[2];//mark[0]表示0在队列中出现的次数,mark[1]同理 
bool check(int x)
{
    memset(mark,0,sizeof(0));
    for(int i=1;i<=x;i++)
    {
        mark[a[i]]++;
    }
    if(mark[0]<=k)//如果一开始就满足的话直接退出 
    {
        head=1;tail=x;//那当然就是从第一个开始到第x个了 
        return 1;
    }
    for(int i=x+1;i<=n;i++)//i表示的是序列的尾巴的位置 
    {
        mark[a[i-x]]--;//去掉开头的那个数 
        mark[a[i]]++;//加上结尾的那个数 
        if(mark[0]<=k) 
        {
            head=i-x+1;tail=i;//尾巴是i,头就是i-x+1 
            return 1;
        }
    }
    return 0;
}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
    scanf("%d",&a[i]);
    l=0;r=n;//长度至少 为0(全是0还不准改),最多为n(全是1) 
    while(l<=r)//标准的二分板子 
    {
        mid=(r+l)>>1;
        if(check(mid))
        {
            ans=mid;
            l=mid+1;
        }
        else
        {
            r=mid-1;
        }
    }
    printf("%d\n",ans);//输出长度 
    for(int i=1;i<=n;i++)
    {
        if(i>=head&&i<=tail)//如果在队列中的话那肯定已经被我们改成1了 
        {
            printf("1 ");
        }
        else
        {
            printf("%d ",a[i]);//不在队列中的话就保持原状 
        }
    }
    return 0;
}
		   		 	  	  												  	


Comments

Submit
0 Comments
More Questions

1546B - AquaMoon and Stolen String
1353C - Board Moves
902A - Visiting a Friend
299B - Ksusha the Squirrel
1647D - Madoka and the Best School in Russia
1208A - XORinacci
1539B - Love Song
22B - Bargaining Table
1490B - Balanced Remainders
264A - Escape from Stones
1506A - Strange Table
456A - Laptops
855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push